{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 主成分分析法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.69884609 -1.63512183  0.041472  ]\n",
      " [ 1.05574935  0.38201416 -0.61956771]\n",
      " [ 0.92259585 -1.18347819 -1.08543643]\n",
      " [-1.5372859  -0.64516989 -0.46616436]\n",
      " [ 0.2090462  -0.43873761  0.46598478]\n",
      " [ 1.31597026 -0.60192008  0.08432748]\n",
      " [-0.60305148 -0.67053247 -0.06342084]\n",
      " [-1.72056024  0.05159737 -0.30965331]\n",
      " [-3.04304363  0.50439662  0.69392477]\n",
      " [-2.92158292  2.55278482 -0.26370719]\n",
      " [-1.13409815  0.57219128 -0.22483092]\n",
      " [ 0.27572554 -0.24052626  0.34786366]\n",
      " [ 1.29962169  0.50357039  0.62069783]\n",
      " [ 1.70175932  0.09027513  1.45544512]\n",
      " [ 3.26297321  1.41283957 -0.43559689]\n",
      " [ 2.31387308  0.98093884 -0.28281   ]\n",
      " [-0.69884609 -1.63512183  0.041472  ]]\n",
      "[0.62686568 0.23366991 0.07003612]\n",
      "[ 2.34507651  1.6598966   1.18980283  0.97582875  0.70768614  0.69019196\n",
      "  0.22578069  0.14100215  0.06115988 -0.53915729 -0.59296961 -0.81725686\n",
      " -0.81725686 -1.08819034 -1.14707691 -1.25340012 -1.74111752]\n",
      "[0.62686568 0.23366991 0.07003612]\n"
     ]
    }
   ],
   "source": [
    "# 例14.6\n",
    "# 结果和书上有些不吻合.\n",
    "\n",
    "import numpy as np\n",
    "from sklearn import preprocessing\n",
    "from sklearn.decomposition import PCA\n",
    "\n",
    "data = np.loadtxt('14.D 宏观投资效益.txt')\n",
    "data = np.divide(np.subtract(data, data.mean(axis=0)), data.std(axis=0, ddof=1))\n",
    "# print(data)\n",
    "# 也可以直接用sklearn的预处理函数, 但这里ddof=1\n",
    "# data = preprocessing.scale(data)\n",
    "\n",
    "'''\n",
    "# 直接使用numpy处理的\n",
    "# 计算关联系数矩阵\n",
    "# np.cov中的bias=False表示除以N-1而不是N\n",
    "R = np.cov(data.T, bias=False)\n",
    "# print(R)\n",
    "eigenvalues, eigenvectors = np.linalg.eig(R)\n",
    "order = eigenvalues.argsort()[::-1]\n",
    "eigenvalues_order = eigenvalues[order]\n",
    "eigenvectors = eigenvectors[order]\n",
    "print(eigenvalues)\n",
    "print(eigenvectors)\n",
    "# 信息贡献率\n",
    "b = np.array([eigenvalues[i]/eigenvalues.sum() for i in range(eigenvalues.shape[0])])\n",
    "print(b)\n",
    "# 累积贡献率\n",
    "alpha = np.array([b[:i+1].sum() for i in range(b.shape[0])])\n",
    "print(alpha)\n",
    "'''\n",
    "\n",
    "# PCA训练器\n",
    "pca = PCA(n_components=3, copy=True, whiten=False)\n",
    "# 训练并降维\n",
    "newX = pca.fit_transform(data)\n",
    "print(newX)\n",
    "# 各个特征的方差百分比\n",
    "print(pca.explained_variance_ratio_)\n",
    "scores = np.matmul(newX, pca.explained_variance_ratio_)\n",
    "print(np.sort(scores)[::-1])\n",
    "print(np.matmul(np.eye(3), pca.explained_variance_ratio_))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
